/**
 * Implementacion de la clase ConfigValidator
 */

#include "configValidator.hpp"

void ConfigValidator::create(std::string &fileToParse, Logger *log, Config *c)
{
	this->setParser(fileToParse,log);
	this->logger = log;
	/* Inicializao con cantidad de campos igual a 2 */
	this->numberOfFieldsExpected = 2;
	this->configItem.setSuccessStatus(3);
	this->config = c;
}

ConfigValidator::ConfigValidator(std::string &fileToParse, Logger *log, Config *c)
{
	this->create(fileToParse,log,c);
}

ConfigValidator::~ConfigValidator()
{
	this->logger = NULL;
	this->numberOfFieldsExpected = 0;
}

void ConfigValidator::validateNumberOfFields(int lineCount, int fieldAmount)
{
	if(fieldAmount != this->numberOfFieldsExpected)
	{
		this->configItem.setFailStatus(3);
		std::string detail = "Cantidad de campos distinta a la esperada";
		this->logger->reportErrorMessage("ConfigValidator","validateNumberOfFields", detail, lineCount);
	}
	else
		this->configItem.setSuccessStatus(3);
}

void ConfigValidator::validateField(int lineCount, int fieldNumber, std::string field)
{
	std::string detail;
	if(fieldNumber == 1)
	{
		if((std::string::npos == field.find_first_of(','))&&(std::string::npos == field.find_first_of('.'))
			&&(field[0] != '0')&&(atoi(field.c_str()) == 0))
		{
			this->configItem.setConfigItemId(field);
		}
		else
		{
			detail = "Esperado un literal, se encontro un real";
			this->logger->reportErrorMessage("ConfigValidator","validateField", detail,lineCount,fieldNumber);
			this->configItem.setFailStatus(fieldNumber);
		}
	}
	else
		this->configItem.setValue(field);

}

void ConfigValidator::validateElementAdding(int lineCount)
{
	bool success = true;
	std::string detail;
	
	if(this->configItem.getConfigItemIdStatus() == 0)
		success = false;
	if(this->configItem.getValueStatus() == 0)
		success = false;

	if(success)
	{
		this->config->addConfigItem(this->configItem.getConfigItemId(),this->configItem.getValue());
		detail = "Item de configuracion agregado, id: "; detail += configItem.getConfigItemId();
		this->logger->reportSuccessMessage("ConfigValidator","validateGraphElementAdding",detail);
	}
	else
	{
		detail = "Item de configuracion descartado";
		this->logger->reportErrorMessage("ConfigValidator","validateGraphElementAdding",detail);
		this->configItem.clear();
	}

}
